package com.atguigu.service;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.AdminRoleDao;
import com.atguigu.dao.RoleDao;
import com.atguigu.entity.AdminRole;
import com.atguigu.entity.Role;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 王健
 * @version 1.0
 */

@Transactional
@DubboService
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService{

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private AdminRoleDao adminRoleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public Map<String, List<Role>> findRoleListMapByadminId(Long adminId) {
        //1.查询所有角色
        List<Role> allRoleList = roleDao.findAll();
        //2.根据当前用户id查询当前用户已分配的所有角色的id
        List<Long> assignRoleIdList = adminRoleDao.findRoleIdListByAdminId(adminId);
        //3.从allRoleList中筛选出用户已分配的角色id
        List<Role> assignRoleList = new ArrayList<>();
        List<Role> unAssignRoleList = new ArrayList<>();
        //3.1遍历处每一个role
        for (Role role : allRoleList) {
            //3.2配短如果role的id在assignRoleIdList，说明当前角色是已分配的
            if (assignRoleIdList.contains(role.getId())) {
                assignRoleList.add(role);
            } else {
                //说明当前角色未分配
                unAssignRoleList.add(role);
            }
        }
        //最后将assignRoleList和UnAssignRoleList放到Map中返回
        Map<String, List<Role>> roleListMap = new HashMap<>();
        roleListMap.put("assginRoleList", assignRoleList);
        roleListMap.put("noAssginRoleList", unAssignRoleList);
        return roleListMap;
    }

/*    @Override
    public void saveAdminRole(Long adminId, List<Long> roleIds) {
        //1.删除当前用户分配的所有角色
        adminRoleDao.deleteByAdminId(adminId);
        //2.将这次需要分配的角色进行新增
        if (!CollectionUtils.isEmpty(roleIds)) {
            for (Long roleId : roleIds) {
                //新增
                AdminRole adminRole = new AdminRole();
                adminRole.setAdminId(adminId);
                adminRole.setRoleId(roleId);
                adminRoleDao.insert(adminRole);
            }
        }
    }*/

    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        //1.删除当前用户分配的所有角色
        adminRoleDao.deleteByAdminId(adminId);
        //2.将这次需要分配的角色进行新增
        for(Long roleId : roleIds) {
            if(StringUtils.isEmpty(roleId)) continue;
            AdminRole userRole = new AdminRole();
            userRole.setAdminId(adminId);
            userRole.setRoleId(roleId);
            adminRoleDao.insert(userRole);
        }
    }

    @Override
    public List<Role> findByAdminId(Long adminId) {
        return roleDao.findByAdminId(adminId);
    }

    @Override
    protected BaseDao<Role> getEntityDao() {
        return roleDao;
    }

}
